#include "mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , btnSelectPic(new QPushButton(QStringLiteral("选择图片")))
    , btnSelectArchive(new QPushButton(QStringLiteral("选择图片")))
    , labSelectPic(new QLabel(QStringLiteral("图片：")))
    , labSelectArchive(new QLabel(QStringLiteral("压缩包：")))
    , edtSelectPic(new QLineEdit())
    , edtSelectArchive(new QLineEdit())
    , btnDoWork(new QPushButton(QStringLiteral("生成")))
{
    initUI();
    initConnect();
}

MainWindow::~MainWindow() {

}

void MainWindow::initUI()
{
    QWidget *wdgMain = new QWidget();
    QHBoxLayout *layoutPic = new QHBoxLayout();
    QHBoxLayout *layoutArchive = new QHBoxLayout();
    QVBoxLayout * layoutMain = new QVBoxLayout();
    edtSelectPic->setDisabled(true);
    edtSelectArchive->setDisabled(true);

    layoutPic->addWidget(labSelectPic);
    layoutPic->addWidget(edtSelectPic);
    layoutPic->addWidget(btnSelectPic);

    layoutArchive->addWidget(labSelectArchive);
    layoutArchive->addWidget(edtSelectArchive);
    layoutArchive->addWidget(btnSelectArchive);
    layoutMain->addLayout(layoutPic);
    layoutMain->addLayout(layoutArchive);
    layoutMain->addWidget(btnDoWork);

    wdgMain->setLayout(layoutMain);
    setCentralWidget(wdgMain);
}

void MainWindow::initConnect()
{
    connect(btnSelectPic,&QPushButton::clicked,this,&MainWindow::slotSelectPic);
    connect(btnSelectArchive,&QPushButton::clicked,this,&MainWindow::slotSelectArchive);
    connect(btnDoWork,&QPushButton::clicked,this,&MainWindow::slotDoWork);
}

void MainWindow::slotSelectPic()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("JPG Files (*.jpg);;All Files (*)"));
    edtSelectPic->setText(fileName);
}

void MainWindow::slotSelectArchive()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("zip Files (*.zip);;All Files (*)"));
    edtSelectArchive->setText(fileName);
}

void MainWindow::slotDoWork()
{
    QString picNamePath = edtSelectPic->text();
    QString ArchiveNamePath = edtSelectArchive->text();
    QFile outFile("out");
    QFile ArchiveFile(ArchiveNamePath);
    QFile picFile(picNamePath);
    if (picFile.open(QIODevice::ReadOnly ) && outFile.open(QIODevice::WriteOnly | QIODevice::Unbuffered) && ArchiveFile.open(QIODevice::ReadOnly)) {

        QByteArray picData = picFile.readAll();
        QByteArray archiveData = ArchiveFile.readAll();
        outFile.write(picData) ;
        outFile.write(archiveData);
    }


     outFile.close();
    ArchiveFile.close();
    picFile.close();
}
